package com.slf.engine.services.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.exception.EngineException;
import com.slf.engine.http.HttpClientManager;
import com.slf.engine.http.HttpResponseWrapper;
import com.slf.engine.services.IJqhService;
import com.slf.engine.utils.KeyedDigestMD5;

public class JqhServiceImpl implements IJqhService {

	private static final Logger log = Logger.getLogger(JqhServiceImpl.class);
	
	public Order doPay(Order order)
	{
		StringBuffer url = new StringBuffer();
		url.append("Action=Change");
		url.append("&UserName=" + SysContants.JQH_LOGINNAME);
		url.append("&Mobile=" + order.getAccount());
		url.append("&OrderID=" + order.getOrderId());
		url.append("&Money=" + Integer.parseInt(order.getMoney())*100);
		url.append("&Key=" + KeyedDigestMD5.getKeyedDigest(url.toString() + SysContants.JQH_KEY, ""));
		
		HttpClientManager manager = new HttpClientManager();
		try {
			log.log(Priority.INFO, "充值提交：" + url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", SysContants.JQH_URL + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body =  new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "充值返回：" + body);
			HtmlCleaner cleaner = new HtmlCleaner();
			TagNode node = cleaner.clean(body);
			if(body.indexOf("ErrorID") != -1)
			{
				Object obj[] = node.evaluateXPath("//root/ErrorID/text()");
				if(obj != null && obj.length > 0)
				{
					StringBuffer s = (StringBuffer)obj[0];
					if("2005".equals(s.toString()))
					{
						order.setClbz(SysContants.CZ_RUN);
					}else{
						order.setClbz(SysContants.CZ_FAILURE);
						obj = node.evaluateXPath("//root/ErrorMsg/text()");
						order.setCljg(((StringBuffer)obj[0]).toString());
					}
				}else{
					order.setClbz(SysContants.CZ_UNKNOW);
					order.setCljg("上游充值提交出现未知错误," + body);
				}
			}else{
				Object obj[] = node.evaluateXPath("//root/Status/text()");
				if(obj != null && obj.length > 0)
				{
					StringBuffer s = (StringBuffer)obj[0];
					if("0".equals(s.toString()))
					{
						order.setClbz(SysContants.CZ_RUN);
						obj = node.evaluateXPath("//root/CodeSN/text()");
						order.setYyslsh(((StringBuffer)obj[0]).toString());
					}else if("1".equals(s.toString())){
						order.setClbz(SysContants.CZ_SUCCESS);
						order.setCljg("充值成功");
					}else if("2".equals(s.toString())){
						order.setClbz(SysContants.CZ_FAILURE);
						order.setCljg("充值失败");
					}else{
						order.setClbz(SysContants.CZ_UNKNOW);
						order.setCljg("上游充值提交出现未知错误,status:" + s.toString());
					}
				}else{
					order.setClbz(SysContants.CZ_UNKNOW);
					order.setCljg("上游充值提交出现未知错误," + body);
				}
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			order = doQuery(order);
			if(order.getClbz() == SysContants.CZ_UNKNOW)
			{
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("上游充值提交出现未知错误");
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	
	public Order doQuery(Order order)
	{
		StringBuffer url = new StringBuffer();
		url.append("Action=SearchOrder");
		url.append("&UserName=" + SysContants.JQH_LOGINNAME);
		url.append("&OrderID=" + order.getOrderId());
		url.append("&Key=" + KeyedDigestMD5.getKeyedDigest(url.toString() + SysContants.JQH_KEY, ""));
		
		HttpClientManager manager = new HttpClientManager();
		try {
			log.log(Priority.INFO, "查询提交：" + url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", SysContants.JQH_URL + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body =  new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "查询返回：" + body);
			
			HtmlCleaner cleaner = new HtmlCleaner();
			TagNode node = cleaner.clean(body);
			
			if(body.indexOf("ErrorID") != -1)
			{
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("上游返回" + body);
			}else{
				Object obj[] = node.evaluateXPath("//root/Status/text()");
				if(obj != null && obj.length > 0)
				{
					StringBuffer s = (StringBuffer)obj[0];
					if("1".equals(s.toString()))
					{
						order.setClbz(SysContants.CZ_SUCCESS);
						order.setCljg("充值成功");
					}else if("2".equals(s.toString())){
						order.setClbz(SysContants.CZ_FAILURE);
						order.setCljg("充值失败");
					}else if("0".equals(s.toString())){
						order.setClbz(SysContants.CZ_RUN);
					}else{
						order.setClbz(SysContants.CZ_UNKNOW);
						order.setCljg("查询返回未知状态，需要核实");
					}
					
				}else{
					return doQuery(order);
				}
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doQuery(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	public static void main(String args[])
	{
		Order order = new Order();
		order.setAccount("13858001723");
		order.setMoney("20");
		order.setOrderId("131854067365726");
		new JqhServiceImpl().doQuery(order);
	}
}
